package com.ciwong.epaper.modules.pad.contentprovider;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.bean.MyGrade;
import com.ciwong.epaper.modules.me.ui.BindPhoneActivity;
import com.ciwong.epaper.modules.me.ui.ResourceCenterH5Activity;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.DataProvider;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.HomeworkWidgetCheckState;
import com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews.GradeRemoteViews;
import com.ciwong.epaper.ui.MainActivity;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;

import static com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants.JUMP_2_ACTIVITY_FLAG;
import static com.ciwong.mobilelib.utils.BaseJumpManager.GO_BACK_ID;


/**
 * 我的成绩  书桌
 *
 * @author Young  created at 2018/8/31 17:22
 */
public class MyGradeWidgetProvider extends AppWidgetProvider {


    /**
     * 广播 action
     */
    public static final String ACTION_WIDGET_LOGIN_GRADE = "WIDGETGRADE_WIDGET_LOGIN";//登录
    public static final String ACTION_REFRESH_CLICK_GRADE = "WIDGETGRADE_REFRESH_CLICK";//点击刷新
    public static final String ACTION_REFRESH_AUTO_GRADE = "WIDGETGRADE_REFRESH_AUTO";//自动刷新
    public static final String ACTION_JUMP_MYGRADE_GRADE = "WIDGETGRADE_JUMP_GRADE";//点击跳转我的成绩
    public static final String ACTION_JUMP_MYBOOK_GRADE = "WIDGETBOOK_JUMP_BOOK";//点击跳转我的书桌
    public static final String ACTION_JUMP_MYRESOURCE_GRADE = "WIDGETRESOUCE_JUMP_RESOUCE";//点击跳转资源中心
    public static final String ACTION_RTFRESH_GRADE_SUCCESS = "ACTION_RTFRESH_GRADE_SUCCESS";


    public static boolean flagLogin = false;
    private ArrayList<MyGrade> myGrades = new ArrayList<MyGrade>();


    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (ACTION_WIDGET_LOGIN_GRADE.equals(intent.getAction())) {//点击登录
            boolean flag_widget_bind_phone = intent.getBooleanExtra(AppWidgetConstants.FLAG_WIDGET_BIND_PHONE, false);
            if (!HomeworkWidgetCheckState.isLogin(context)) {
                LoginToken.goToUnifiedLogin2(context);
            } else if (!flag_widget_bind_phone) {
                jumpToBindPhone(context);
            }

        } else if (ACTION_REFRESH_CLICK_GRADE.equals(intent.getAction())) {//点击刷新
            // Toast.makeText(context, "接到广播： 点击刷新 " + intent.getAction(), Toast.LENGTH_LONG).show();
            if (EApplication.getInstance() == null || EApplication.getInstance().getUserInfoBase() == null) {
                refreshView(context, myGrades, HomeworkWidgetCheckState.isLogin(context));
                return;
            }
            if (NetworkUtils.isOnline()) {
                loading(context, true);
                refresh(context);
            } else {
                ToastUtil.INSTANCE.toastCenterNoNetError();
            }

        } else if (LoginToken.ACTION_LOGIN_SUCCESS.equals(intent.getAction())) {
            boolean flag_widget_bind_phone = intent.getBooleanExtra(LoginToken.IS_BIND_PHONE, false);
            if (flag_widget_bind_phone) {
                refresh(context);
                loginSuccess(context, intent);
            } else {
                jumpToBindPhone(context);
            }

            //  Toast.makeText(context, "接到广播： 登录成功  " + intent.getAction(), Toast.LENGTH_LONG).show();
        } else if (LoginToken.ACTION_BIND_PHONE_SUCCESS.equals(intent.getAction())) {//接收绑定数据成功（app内部）
            refresh(context);
            loginSuccess(context, intent);
        } else if (ACTION_REFRESH_AUTO_GRADE.equals(intent.getAction())) {//自动刷新
            // Toast.makeText(context, "接到广播： 自动刷新 " + intent.getAction(), Toast.LENGTH_LONG).show();
            refresh(context);

        } else if (ACTION_RTFRESH_GRADE_SUCCESS.equals(intent.getAction())) {
            myGrades = (ArrayList<MyGrade>) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_MYGRADE);
            refreshView(context, myGrades, HomeworkWidgetCheckState.isLogin(context));
            // Toast.makeText(context, "接到广播： 刷新成功 " + intent.getAction(), Toast.LENGTH_LONG).show();


        } else if (LoginToken.ACTION_EXIT_LOGIN.equals(intent.getAction())) {//接收到退出登录
            logoutSuccess(context, intent);
        } else if (ACTION_JUMP_MYGRADE_GRADE.equals(intent.getAction())) {//跳转我的成绩
            //Toast.makeText(context, "接到广播： 跳转我的成绩 " + intent.getAction(), Toast.LENGTH_LONG).show();
        } else if (ACTION_JUMP_MYBOOK_GRADE.equals(intent.getAction())) {//点击跳转我的书桌
            jumpToBook(context);
            // Toast.makeText(context, "接到广播： 点击跳转我的书桌 " + intent.getAction(), Toast.LENGTH_LONG).show();
        } else if (ACTION_JUMP_MYRESOURCE_GRADE.equals(intent.getAction())) {//点击跳转资源中心
            //Toast.makeText(context, "接到广播： 点击跳转资源中心 " + intent.getAction(), Toast.LENGTH_LONG).show();
            jumpToResource(context);
        } else if ("android.intent.action.SCREEN_ON".equals(intent.getAction())) {//接收到亮屏广播
            refresh(context);
        } else {
            //  Toast.makeText(context, "接到广播： 未知广播 " + intent.getAction(), Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        GradeRemoteViews gradeRemoteViews = new GradeRemoteViews(context);
        // 更新所有的widget

        gradeRemoteViews.checkIsLogin(HomeworkWidgetCheckState.isLogin(context), HomeworkWidgetCheckState.isBindPhone(), myGrades);

        if (HomeworkWidgetCheckState.isLogin(context) && HomeworkWidgetCheckState.isBindPhone()) {
            refresh(context);
        }
        appWidgetManager.updateAppWidget(appWidgetIds, gradeRemoteViews);

    }

    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
    }


    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
    }

    /**
     * @param context
     * @param
     */
    private void jumpToBook(Context context) {
        if (EApplication.getInstance() == null || EApplication.getInstance().getUserInfoBase() == null) {
            refreshView(context, myGrades, HomeworkWidgetCheckState.isLogin(context));
            return;
        }
        Intent newIntent = new Intent(context, MainActivity.class);
        newIntent.putExtra(JUMP_2_ACTIVITY_FLAG, AppWidgetConstants.JUMP_2_ACTIVITY_FLAG_BOOK_DESK);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(newIntent);
    }

    private void jumpToBindPhone(Context context) {
        if (EApplication.getInstance() == null || EApplication.getInstance().getUserInfoBase() == null) {
            refreshView(context, myGrades, HomeworkWidgetCheckState.isLogin(context));
            return;
        }
        Intent newIntent = new Intent(context, BindPhoneActivity.class);
        newIntent.putExtra(GO_BACK_ID, R.string.go_back);
        newIntent.putExtra(IntentFlag.INTENT_FLAG_STR, "");
        newIntent.putExtra(IntentFlag.INTENT_FLAG_ID, "2");//代表登录绑定
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(newIntent);
    }

    private void jumpToResource(Context context) {
        if (EApplication.getInstance() == null || EApplication.getInstance().getUserInfoBase() == null) {
            refreshView(context, myGrades, HomeworkWidgetCheckState.isLogin(context));
            return;
        }
        Intent newIntent = new Intent(context, ResourceCenterH5Activity.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(newIntent);
    }

    private void refresh(Context context) {
    }

    /**
     * 退出登录
     *
     * @param context
     * @param intent
     */
    private void logoutSuccess(Context context, Intent intent) {
        refreshView(context, myGrades, false);
    }

    /**
     * 登录成功
     *
     * @param context
     * @param intent
     */
    private void loginSuccess(Context context, Intent intent) {
        refreshView(context, myGrades, true);
    }


    /**
     * 刷新整个界面
     *
     * @param context
     * @param myGrades
     * @param isLogin
     */
    private void refreshView(Context context, ArrayList<MyGrade> myGrades, boolean isLogin) {
        GradeRemoteViews gradeRemoteViews = new GradeRemoteViews(context);
        gradeRemoteViews.checkIsLogin(isLogin, HomeworkWidgetCheckState.isBindPhone(), myGrades);
        gradeRemoteViews.loadComplete();
        gradeRemoteViews.updateAppWidget();
    }


    public void loading(Context context, boolean isLoading) {
        GradeRemoteViews gradeRemoteViews = new GradeRemoteViews(context);
        if (isLoading) {

            gradeRemoteViews.loading();

        } else {
            gradeRemoteViews.loadComplete();
        }

    }


}
